package com.xiaotu.spider.guduo.pageprocessor;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.GuDuoSetsPlayCountModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.GsonUtils;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.common.util.UUIDUtils;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
/***
 * 骨朵剧分集播放量任务
 * @author Mr.zhang
 *
 */
public abstract class GuDuoSetsPlayCountPageProcessor extends AUrlPageProcessor {
	
	protected Map<String,String> headers = null;
	
	{
		headers = new HashMap<String,String>();
	    headers.put("Accept", PropertiesUtil.getProperty("GuDuo.site.Accept"));
	    headers.put("Accept-Encoding", PropertiesUtil.getProperty("GuDuo.site.Accept-Encoding"));
	    headers.put("Accept-Language", PropertiesUtil.getProperty("GuDuo.site.Accept-Language"));
	    headers.put("Cache-Control", PropertiesUtil.getProperty("GuDuo.site.Cache-Control"));
	    headers.put("Connection", PropertiesUtil.getProperty("GuDuo.site.Connection"));
	    headers.put("Cookie", PropertiesUtil.getProperty("GuDuo.site.Cookie"));
	    headers.put("Host", PropertiesUtil.getProperty("GuDuo.site.Host"));
	    headers.put("User-Agent", PropertiesUtil.getProperty("GuDuo.site.User-Agent"));
	}
	
	private String [] start_url = {"https://www.baidu.com/"};
	
	protected String play_count_url = "http://ev.guduodata.com/playcount/episode/%s";
	
	@Override
	public void process(Page page) {
		
		Map<String, Object> extrasMap = page.getRequest().getExtras();
		
		switch (this.getPageDataType(page))
        {
            case "episode":
                this.playCountProcess(page,extrasMap);
                break;
            default:
            	this.addRequestProcess(page);
            	break;
        }
	}
	
	//获取播放量
	@SuppressWarnings("unchecked")
	private void playCountProcess(Page page, Map<String, Object> extrasMap) {
		
		List<GuDuoSetsPlayCountModel> pipeList = new ArrayList<GuDuoSetsPlayCountModel>();
		
		String tv_dataid = extrasMap.get("tv_dataid").toString();
		String tvname = extrasMap.get("tvname").toString();
		String json = page.getRawText();
		if (StringUtils.isEmpty(json))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        Map<String, Object> map = GsonUtils.fromJson(json, Map.class);
        if (map == null || map.isEmpty() || map.get("counts") == null)
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
		List<Double> countsList = (List<Double>) map.get("counts");
		List<String> episodesList = (List<String>) map.get("episodes");
        if (countsList == null || countsList.isEmpty()
        		|| episodesList == null || episodesList.isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        if(countsList.size() == episodesList.size()){
        	for(int i = 0;i<countsList.size();i++){
        		GuDuoSetsPlayCountModel model = new GuDuoSetsPlayCountModel(
    					UUIDUtils.getId(), tv_dataid, tvname, Constants.SiteName.GUDUO, new BigDecimal(episodesList.get(i)).toPlainString(),
    					new BigDecimal(countsList.get(i)).toPlainString(), System.currentTimeMillis()
    					);
    			pipeList.add(model);
        	}
        }
        page.putField(GuDuoSetsPlayCountModel.class.getName(), pipeList);
	}
	
	//添加请求
	private void addRequestProcess(Page page){
		//每日从骨朵列表爬取
		addBusinessProcess(page);
	}
	
	
	//剧列表
	protected abstract void addBusinessProcess(Page page);
	
	@Override
	protected String[] getStartRequestUrls() {
		return start_url;
	}

	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.TV_GUDUO;
	}

	@Override
	public String getPageDataType(Page page) {
		String url = page.getRequest().getUrl();
		if(url.contains("playcount/episode")){
			return "episode";
		}
		return "addRequest";
	}
	
}
